package zl.controller;

import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import zl.bean.Novel;
import zl.service.NovelService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api/excel")
public class ExcelController {

    @Autowired
    private NovelService novelService;

    @RequestMapping()
    protected void doPost(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName="小说信息.xlsx";
        fileName= URLEncoder.encode(fileName, "utf-8");
        response.setHeader("Content-disposition", "attachment;filename="+fileName);
        List<Novel> novels=new ArrayList<>();
        novels=novelService.selectAllNovel();
        EasyExcel.write(response.getOutputStream(),Novel.class).sheet("novel").doWrite(novels);
    }
}
